<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once(SERVER_FS_ROOT."/app/_inc/include.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");

$errMsg = sxREQ('msg');

$objUser = $_SESSION[USEROBJ];
$aDataArea = $objUser->getDataArea();
$nomeFile = sxREQUEST('nf');
$aImpTab = array();
$aConfTab = array();
$headPref = $timeExp = $dbTypeExp = $hostExp = $dbNameExp = '';
$serImpTab = file_get_contents(SERVER_FS_TEMP."/imex/$nomeFile.txt", "rb");
if($serImpTab === false) {
	$errMsg = _t("Unable to read file %s",SERVER_FS_TEMP."/imex/$nomeFile.txt");
} elseif (substr($serImpTab,0,10) != "POLARIS-MB" ) {
	$errMsg = "Wrong file";
} else {
	list($headPref, $dateExp, $hourExp, $dbTypeExp, $hostExp, $dbNameExp, $serverExp, $computerExp) = 
		explode(" ",substr($serImpTab,0,128));
	if ($hostExp == 'localhost') {$hostExp = $serverExp;}
	if ($hostExp == 'localhost') {$hostExp = $computerExp;}
	$aImpTab = unserialize(substr($serImpTab,128));
	if (sxREQUEST('reqtype')=='apply') {
		$errMsg .= applyImp($aImpTab);
	}
	$aConfTab = tabCompare($aImpTab);
}

$aOpz = array("kMenu"=>'uti.cif', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));

$aCmd = array(
	"applica"=>array("pag"=>"javascript:document.objFrm.submit();",
						"title"=>_t("apply"),
						"text"=>_t("apply")
						),
	"back"=>array()
	);
echo(commandsHTML($aCmd));	
echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"".$_SERVER['PHP_SELF']."\" method=\"POST\" >");
echo("<input type=\"hidden\" name=\"nf\" id=\"nf\" value=\"".sxREQUEST('nf')."\">");
echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"apply\">");
echo("<input type=\"hidden\" name=\"xsn\" value=\"".XSN."\">");
echo("<tr>" .
			"<td class=\"titl01\" colspan ='20'>File exported ".YMD2DMY($dateExp)." at $hourExp from $dbNameExp@$hostExp ($dbTypeExp)</td>" .
		"</tr>\n");
echo("<tr>" .
	"<td class=\"tabh01\" width='10%'>Ref. table</td>" .
	"<td class=\"tabh01\" width='10%'>Ref. key</td>" .
	"<td class=\"tabh01\" width='10%'>Var. table</td>" .
	"<td class=\"tabh01\" width='10%'>Var. key</td>" .
	"<td class=\"tabh01\" width='60%' colspan='2'>Var. field</td>" .
	"</tr>\n");
$nTab = 0;
foreach($aConfTab as $tabRef => $aTabRef) {
	foreach($aTabRef as $keyRef => $aTabImp) {
		echo("<tr>" .
					"<td class=\"text01\" valign='top'>$tabRef </td>" .
					"<td class=\"text01\" valign='top'>".str_replace('@','<br>',$keyRef).
					"<td class=\"text01\" colspan='4' valign='top'>".
					((count($aTabImp) == 1)?"-- no changes --":"&nbsp;")."</td>" .
					"</td>" .
			"</tr>\n");
		foreach($aTabImp as $tab => $aKeys) {
			$nTab++;
			$sTab = str_pad($nTab,6,'0',STR_PAD_LEFT);
			echo("<input type='hidden' name='ntab_$sTab' value='$tab'>\n");
			$nKey = 0;
			foreach($aKeys as $keyImp => $aVariaz) {
				//dbg_("tab=$tab - key=$keyImp - ".print_r($aVariaz,1));
				if (isset($aVariaz['op'])) {
					$nKey++;
					if ($aVariaz['op'] == 'ins') {
						$sVar = '<b>inserisce</b>';
						$sCB = "<input type='hidden' name='nkey_".$sTab."_ins_".$nKey."' value=".sxHE($keyImp).">&nbsp;";
					} elseif ($aVariaz['op'] == 'del') {
						$sVar = '<b>cancella</b>';
						$sCB = "<input type='checkbox' name='nkey_".$sTab."_del_".$nKey."' value=".sxHE($keyImp)." class='text01'>";
					} else {
						$sCB = "<input type='checkbox' name='nkey_".$sTab."_mod_".$nKey."' value=".sxHE($keyImp)." class='text01'>";
						$sVar = '<b>modifica</b>';
						foreach($aVariaz['var'] as $z3 => $sMod) {
							$sVar .= "<br>".$sMod;
						}
					}					
					echo("<tr>" .
								"<td class=\"text01\">&nbsp;</td>" .
								"<td class=\"text01\">&nbsp;</td>" .
								"<td class=\"text01\" valign='top'>$tab</td>" .
								"<td class=\"text01\" valign='top'>".str_replace('@','<br>',$keyImp)."</td>" .
								"<td class=\"text01\" valign='top'>".$sCB."</td>" .
								"<td class=\"text01\" valign='top'>$sVar</td>" .
						"</tr>\n");
				}
			}
		}
	}
}
echo("</form>");
echo("</table>");
echo(commandsHTML($aCmd));
echo(footerHTML());
exit();

function applyImp(&$aImpTab) {
	global $objUser;
	$objUser->setHistoryVar(array('reqtype'=>''));
	$aNtab = array();
	$aKeyVar = array();
	foreach($_REQUEST as $nom => $val) {
		if (substr($nom,0,5)== 'ntab_') {
			$aNtab[substr($nom,5)] = $val;
			$objUser->setHistoryVar(array($nom=>''));
		}
	}
	foreach($_REQUEST as $nom => $val) {
		if (substr($nom,0,5)== 'nkey_') {
			$tab = $aNtab[substr($nom,5,6)];
			$aKeyVar[$tab][$val] = substr($nom,12,3);
			$objUser->setHistoryVar(array($nom=>''));
		}
	}
	//dbg_("aKeyVar=".print_r($aKeyVar,1));
	$errMsg = tabImport($aImpTab, $aKeyVar);
	if ($errMsg != '') { return $errMsg; }
	return "Data imported";
}
